package com.curtain.koreyoshi.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.curtain.koreyoshi.bean.AdData;
import com.curtain.koreyoshi.utils.ResUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

public class HorizontalPullDoorChargeView extends AdBaseView {


	private Scroller mScroller;

	private int mLastDownX = 0;

	private int mCurryX;

	private int mDelX;

	private boolean mCloseFlag = false;

	//渐变背景
	private LinearLayout backLayout;
	//系统时间
	private TextView tvSysTime;
	//滑动解锁
	private TextView tvHint;
	//电量
	private TextView tvBattery;
	//剩余时间
	private TextView tvLeftTime;
	//广告图
	private ImageView iv_ad_pic;

	public HorizontalPullDoorChargeView(Context context, AdData adData) {
		super(context, adData);
	}

	public HorizontalPullDoorChargeView(Context context, AdData adData,float battery) {
		super(context, adData,battery);
	}


	@Override
	public void initView() {
		int width;
		int height;

		if(screenWidth > screenHeight) {
			width = (int) (screenHeight*0.8);
		} else {
			width = (int) (screenWidth*0.8);
		}
		height = (int) (width *(mAdData.getHeight()*1.0 / mAdData.getWidth()));
// 这个Interpolator你可以设置别的 我这里选择的是有弹跳效果的Interpolator  new BounceInterpolator()
		Interpolator polator = new LinearInterpolator();
		mScroller = new Scroller(mContext, polator);

		// 这里你一定要设置成透明背景,不然会影响你看到底层布局
		this.setBackgroundColor(Color.argb(0, 0, 0, 0));
		setOrientation(LinearLayout.VERTICAL);
		tvSysTime = new TextView(mContext);
		tvSysTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtil.heightDip2px(72));
		tvSysTime.setTextColor(0xffffffff);
		tvSysTime.setPadding(ResUtil.widthDip2px(40),ResUtil.heightDip2px(28),0,ResUtil.heightDip2px(28));
		tvSysTime.setBackgroundColor(Color.parseColor("#212b53"));
		LayoutParams timeL = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		addView(tvSysTime,timeL);

		backLayout = new LinearLayout(mContext);
		backLayout.setGravity(Gravity.CENTER_HORIZONTAL);
		backLayout.setOrientation(LinearLayout.VERTICAL);

		LinearLayout batLayout = new LinearLayout(mContext);
		LayoutParams batL = new LayoutParams(LayoutParams.MATCH_PARENT,0,1);
		batLayout.setOrientation(LinearLayout.VERTICAL);
		batLayout.setGravity(Gravity.CENTER);


		LayoutParams tvBatL = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		tvBattery = new TextView(mContext);
		tvBattery.setTextColor(0xffffffff);
		tvBattery.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtil.heightDip2px(150));
		batLayout.addView(tvBattery,tvBatL);

		tvLeftTime = new TextView(mContext);
		tvLeftTime.setTextColor(0xffffffff);
		tvLeftTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtil.heightDip2px(30));
		batLayout.addView(tvLeftTime,tvBatL);

		backLayout.addView(batLayout,batL);

		iv_ad_pic = new ImageView(mContext);
		LayoutParams ivL = new LayoutParams(width,height);
		backLayout.addView(iv_ad_pic,ivL);

		tvHint = new TextView(mContext);
		tvHint.setText("滑动解锁  >");
		tvHint.setTextColor(0xffffffff);
		tvHint.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResUtil.heightDip2px(48));
		LayoutParams tvL = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		tvL.topMargin = ResUtil.heightDip2px(32);
		tvL.bottomMargin = ResUtil.heightDip2px(52);
		setTvAnim();
		backLayout.addView(tvHint,tvL);

		backLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		GradientDrawable background = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,new int[]{Color.parseColor("#001875"),Color.parseColor("#2a51a7"),Color.parseColor("#3e71e4"),Color.parseColor("#172236")});
		background.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		backLayout.setBackgroundDrawable(background);
		addView(backLayout);

	}

	@Override
	public void initData() {
		mImageLoader.DisplayImage(mAdData.getRawImageUrl(),iv_ad_pic,true);

		Date d = new Date();
		SimpleDateFormat ss = new SimpleDateFormat("HH:mm");//12小时制
		String format = ss.format(d);
		System.out.println(format);
		tvSysTime.setText(format);
		tvBattery.setText((int)(battery*100) + "%");
		tvLeftTime.setText("正在为您快速充电哦");

		iv_ad_pic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mOnAdClickListener != null){
					mOnAdClickListener.onAdClicked(mAdData);
				}
			}
		});
	}



	
	public void setTvAnim(){
		Animation ani = new AlphaAnimation(0.2f, 1f);
		ani.setDuration(1500);
		ani.setRepeatMode(Animation.REVERSE);
		ani.setRepeatCount(Animation.INFINITE);
		tvHint.startAnimation(ani);
	}


	// 推动门的动画
	public void startBounceAnim(int startX, int dx, int duration) {
		mScroller.startScroll(startX, 0, dx, 0, duration);
		invalidate();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			mLastDownX = (int) event.getX();
			System.err.println("ACTION_DOWN=" + mLastDownX);
			return true;
		case MotionEvent.ACTION_MOVE:
			mCurryX = (int) event.getX();
			System.err.println("ACTION_MOVE=" + mCurryX);
			mDelX = mCurryX - mLastDownX;
			// 只准上滑有效
			if (mDelX > 0) {
				scrollTo(-mDelX, 0);
			}
			System.err.println("-------------  " + mDelX);

			break;
		case MotionEvent.ACTION_UP:
			mCurryX = (int) event.getX();
			mDelX = mCurryX - mLastDownX;
			if (mDelX > 0) {

				if (Math.abs(mDelX) > screenWidth / 2) {

					// 向上滑动超过半个屏幕高的时候 开启向上消失动画
					startBounceAnim(this.getScrollX(), this.getScrollX() - screenWidth, 450);
					mCloseFlag = true;
				} else {
					// 向上滑动未超过半个屏幕高的时候 开启向下弹动动画
					startBounceAnim(this.getScrollX(), -this.getScrollX(), 450);

				}
			}

			break;
		}
		return super.onTouchEvent(event);
	}

	@Override
	public void computeScroll() {

		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			// 不要忘记更新界面
			postInvalidate();
		} else {
			if (mCloseFlag) {
				this.setVisibility(View.GONE);
				if (mOnAdClickListener != null){
					mOnAdClickListener.onAdRemoved(mAdData);
				}
			}
		}
	}


}
